<template>
  <div class="list-content">
    <el-row type="flex" justify="space-between">
      <el-form :inline="true" :model="formInline" class="demo-form-inline" @submit.native.prevent>
        <el-form-item>
          <el-select filterable @change="getList(1)" v-model="formInline.fireSystem" placeholder="请选择消防系统">
            <el-option
              v-for="item in roleOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input 
          v-model.trim="formInline.name" 
          placeholder="请输入资产类别名称"
          @keyup.enter.native="getList(1)"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button class="form-search-btn" type="primary" @click="getList(1)">查询</el-button>
        </el-form-item>
      </el-form>
      <div class="more-btns">
        <el-button type="primary" @click="addPlan()">新增</el-button>
      </div>
    </el-row>
    <el-table
      v-loading="loading"
      :data="tableData"
      ref="mainTable"
      style="width: 100%">
      <el-table-column
      label="序号"
      type="index"
      width="50">
      </el-table-column>
      <el-table-column prop="assetCategoryPicUrl" label="类别图片" width="100" >
        <!-- 图片的显示 -->
        <template  slot-scope="scope">            
          <img :src="scope.row.assetCategoryPicUrl?scope.row.assetCategoryPicUrl.split(',')[0]:defaultImg"  width="50" height="50" @click="$imgPreview" :data-imgpreview="scope.row.assetCategoryPicUrl" />
        </template>         
      </el-table-column>
      <el-table-column
      prop="name"
      label="资产类别">
      </el-table-column>
      <el-table-column
      prop="fireSystemName"
      label="消防系统">
      </el-table-column>
      <el-table-column
      prop="remarks"
      label="备注">
      </el-table-column>
      <el-table-column 
      label="操作"
      width="140">
        <template slot-scope="scope">
          <el-button  @click="addPlan(scope.$index, scope.row)" type="text" size="small">修改</el-button>
          <span class="dividing-line">|</span>
          <el-button  @click="delPlan(scope.$index, scope.row)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      @size-change="sizeChange"
      @current-change="pageChange"
      :current-page="formInline.currentPageIndex"
      :page-sizes="[10, 50, 100]"
      :page-size="10"
      layout="total, sizes, prev, pager, next, jumper"
      :total="formInline.currentPageTotal">
    </el-pagination>
    <el-dialog
      :close-on-click-modal="false"
      title="资产类别维护"
      width="600px"
      top='10vh'
      v-if="dialogFalg"
      :visible.sync="dialogFalg"
      @close="closeDialog"
      v-dialogDrag>
      <edit-form :editInfo="editInfo" :closeDialog="closeDialog"></edit-form>
    </el-dialog>
  </div>
</template>

<script>
import * as Dao from '@/api/assets'
import util from '@/utils/util'
import editForm from './Dialog/AssetClassLibForm'

export default {
  name: 'AssetClassLib',
  props:['param'],
  components: { 
    editForm
  },
  data () {
    return {
      formInline: {
        organizationId:0,
        name:'',
        fireSystem:'',
        currentPageSize: 10,
        currentPageIndex: 1,
        currentPageTotal: 0
      },
      util:util,
      roleOptions: [{id: '',name: "全部消防系统"}],
      isMuilUnitUser:this.$store.state.userinfo.isMuilUnitUser,
      accountName:this.$store.state.userinfo.accountName,
      defaultImg: this.GLOBAL.defaultImg,
      tableData: [],//表格数据
      detailObj:{},//展开详情
      loading: false,
      dialogFalg: false,
      editInfo: {  }// 编辑对应的表单信息
    }
  },
  created:function() {
    let that = this
    //绑定下拉框
    util.ilinkArrHandle.delDicArr_keyVal(55, function(arr){
      that.roleOptions.push.apply(that.roleOptions, arr);
    })
    this.getList()    
  },
  methods: {
    sizeChange(_pageSize) {//页码切换
      this.getList(1,_pageSize) 
    },
    pageChange(_pageIndex) {//翻页
      this.getList(_pageIndex) 
    },
    getList(_pageIndex, _pageSize) {//获取列表
      if(_pageIndex){
        this.formInline.currentPageIndex = _pageIndex
      }
      if(_pageSize){
          this.formInline.currentPageSize = _pageSize
      }
      this.loading =true;
      Dao.getACs({
        queryType:0,//0或空表示查询系统类别树结构、1表示查询联网单位自定义类别树结构、2表示查询联网单位自定义+系统类别树结构
        pageType:1,//0或空不做分页 1做分页
        organizationId: this.formInline.organizationId,
        pageIndex: this.formInline.currentPageIndex -1,
        pageSize: this.formInline.currentPageSize,
        name: this.formInline.name.trim(),
        fireSystem: this.formInline.fireSystem,
      }).then((result) => {
        this.formInline.currentPageTotal = result.total;
        this.tableData = result.data;
        this.loading =false;
      }) 
    },
    addPlan(index, row) {
      this.dialogFalg = true
      this.editInfo = row||{}
    },
    delPlan(index, row) {
      let that = this
      this.$confirm("确定删除？", '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        roundButton: true
      }).then(() => {
        Dao.delAc({id: row.id}).then(res => {
          if( res.status === 200) {
            that.$message({
              type: 'success',
              message: '删除成功!'
            })
            that.getList(1)
          }else{
            that.$message.error(res.info);
          }
        })
      }).catch(() => {

      })
    },
    closeDialog(refresh) {
      this.dialogFalg = false
      if(refresh) {
        this.getList(this.editInfo.id?'':1)
      }
    },
  },
}
</script>